package cn.fintecher.pangolin.service.business.scheduled;

import cn.fintecher.pangolin.common.enums.MessageType;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.CaseFollowupRecord;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.CaseFollowRecordElastic;
import cn.fintecher.pangolin.service.business.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@Slf4j
public class FollowReminderScheduled {

    @Autowired
    private CaseFollowRecordElastic caseFollowRecordElastic;

    @Autowired
    private BaseService baseService;

    @Scheduled(cron = "0 0 1 * * ?")
    void followReminderScheduled()  {
        log.info("待跟进提醒调度任务开始:{}",ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("follNextDate").gte(ZWDateUtil.getNowDate().getTime()));
        for (CaseFollowupRecord caseFollowupRecord : caseFollowRecordElastic.search(boolQueryBuilder)) {
            baseService.sendMessageToOne(MessageType.FOLLOW_REMINDER, "计划跟进提醒", "尊敬的用户:您计划对客户:" + caseFollowupRecord.getPersonalName() + "于今日进行跟进。", caseFollowupRecord.getOperatorId());
        }
        log.info("待跟进提醒调度任务结束:{}",ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
    }
}
